
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.join;


import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import poolint.push.tablename.IntegratePool;

import utility.CapitalChar;

import control.RequestParameter;

import dbmanager.DBManager;
import dbmanager.DBOperation;
import dbmanager.GetIdName;

public abstract class Joinoperation 
{
	
	protected String recentTableObject = "";
	String process = "add";
	protected String tableObj = "";
	protected String fieldTable = "table";
	protected String fieldTableName = "tablename";
	protected String fieldTablist = "tablelist";
	protected String fieldList = "fieldlist";
	String DATABASE = "database";
	protected String field = "field";
	protected String column = "column";
	protected String property_details = "property_details";
	String connObjId;
	RequestParameter rqp;
	
	DBOperation integratedDB;
	Connection conn;
	protected GetIdName gin ;
	protected Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
	protected Hashtable<Object, Object> poolHash = new Hashtable<Object, Object>();
	protected Hashtable<Object, Object> ModuleHash = new Hashtable<Object, Object>();
	
	Vector<Object> oldPropertyVect = new Vector<Object>();
	Vector<Object> propertyVect = new Vector<Object>();
	protected Vector<Object> poolVector = new Vector<Object>();
	protected Vector<Object> uniquePoolVector = new Vector<Object>();
	protected String masterObj;
	protected String poolTablename;
	
	public Joinoperation() {
		gin = new GetIdName();
	}
	
	public void setTableObj(String tableObj) 
	{
		this.tableObj = tableObj;		
	}
	
	public void setMaster(String masterObj) 
	{
		this.masterObj = masterObj;		
	}
	
	public void setRecentTableObject(String recentTableObject) 
	{
		this.recentTableObject = recentTableObject;
	}
	public void setRequestParam() 
	{
		rqp = new RequestParameter();
		rqp.setUsername("+911234567891");
		rqp.setPassword("891");
		rqp.setProcess(process);
		rqp.setVersion("11");
		rqp.setMessage("");
		rqp.setRequest("master_table");
		rqp.setEvent(false);
	}
	
	public void generateProperty() 
	{
		/**It will fetch the value from pool hash and append the column values in field hash.*/		
		Enumeration<Object> poolIte = poolHash.keys();
		while(poolIte.hasMoreElements())
		{
			String key=null;
			try 
			{
				key = poolIte.nextElement().toString();
				if(ModuleHash.containsKey(key))
				{
					String value = poolHash.get(key).toString();
					fieldHash.put(key, value);
				}
			} catch (Exception e) {
			}
		}
	}
	
	public void generateModuleHash()
	{
		
		/**Setting parameters for getting connection object.**/
		//String tableObjId = gin.getId(tableObj);
		
		
		/**Setting parameters for getting field list from table name.*/
		String fieldId = gin.getId(field);
		String columnId = gin.getId(column);
		String fieldListId = gin.getId(fieldList);
		
		String query = "SELECT DISTINCT d6.id,d6.mid, d6.pid, d6.pv, d6.vt FROM " + property_details + " d3 JOIN (" + property_details + " d4 JOIN (" + property_details + " d5 JOIN " + property_details;
		query = query + " d6 ON (d6.mid = d5.pv)) ON (d5.mid = d4.pv)) ON (d3.mid = d4.mid) ";
		query = query + "WHERE d3.mid = " + tableObj + " AND ";
		query = query + "d4.pid = " + fieldListId + " AND d5.pid = " + fieldId + " AND (d6.pid = " + fieldId +" OR d6.pid = " + columnId + ") order by d6.mid, d6.pid";
		
		System.out.println(" Query : " + query);
		ResultSet rsField = DBManager.getSelect(query);
		
		try 
		{
			String field = null;
			while(rsField.next())
			{
				String mid1 = rsField.getString(2);
				String  poolDefinition = gin.getItem(rsField.getString("pv")).toLowerCase();
				field = rsField.getString("pid");
				rsField.next();
				String mid2 = rsField.getString(2);
				if(!mid1.equals(mid2))
					continue;
				String tableDefinition= gin.getItem(rsField.getString("pv")).toLowerCase();
				
				if(field.equals(fieldId))
					ModuleHash.put(poolDefinition,tableDefinition);
				else
					ModuleHash.put(tableDefinition,poolDefinition);
			}
			rsField.close();
		}
		catch (Exception e) 
		{
			System.out.println("Exception::Integration:: PoolConnection.java, while fetching Fields and Columns ");
			e.printStackTrace();
		}
	}
		
	
	@SuppressWarnings("unchecked")
	public Vector<Object> getPropertyVector()
	{
		generateModuleHash();	
		getPoolHash();
			for (Object obj : poolVector)
			{
				poolHash.putAll((Hashtable<Object, Object>)obj);
				generateProperty();
				
				try
				{
					Class c = Class.forName("poolint.push.tablename."+CapitalChar.makeFirstCharCapital(poolTablename));
					IntegratePool integrate = (IntegratePool) c.newInstance();	
					integrate.setTableObj(tableObj);		
					integrate.setRecentTableObject(recentTableObject);					
					integrate.setMaster(masterObj);					
					integrate.setFieldHash(fieldHash);
					propertyVect.addAll(integrate.getPropertyVector());			
				} catch (Exception e) {
					// TODO: handle exception
				}
			}				
		
		return propertyVect;
	}
	
	public void generateOldPropertyHash()
	{
		try
		{
			Class c = Class.forName("poolint.push.tablename."+CapitalChar.makeFirstCharCapital(poolTablename));
			IntegratePool integrate = (IntegratePool) c.newInstance();	
			//integrate.setTableObj(tableObj);
			integrate.setRecentTableObject(recentTableObject);
			//integrate.setModuleHash(ModuleHash);
			//integrate.setFieldHash(fieldHash);
			poolHash.putAll(integrate.getIntegrateOldPropertyHash());	
			
			//poolVector.addAll(integrate.getPoolVector());
			//poolVector.add(integrate.getPoolVector());
			
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
	
	
	public void getOldPropertyVector()
	{
		try
		{
			Class c = Class.forName("poolint.push.tablename."+CapitalChar.makeFirstCharCapital(poolTablename));
			IntegratePool integrate = (IntegratePool) c.newInstance();	
			integrate.setTableObj(tableObj);
			integrate.setRecentTableObject(recentTableObject);
			//integrate.setModuleHash(ModuleHash);
			integrate.setFieldHash(fieldHash);
			//integrate.setMaster(gin.getId(masterObj));
			integrate.setMaster(masterObj);
			oldPropertyVect.addAll(integrate.getIntegrateOldPropertyVector());			
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
	
	
	public void setFields(Hashtable<Object, Object> fieldHash2) 
	{
		fieldHash.putAll(fieldHash2);
	}
	
	public void setPoolHash(Hashtable<Object, Object> poolHash2) 
	{
		poolHash.putAll(poolHash2);
	}
	
	public void setModuleHash(Hashtable<Object, Object> moduleHash2) 
	{
		ModuleHash.putAll(moduleHash2);
	}
	
	public Hashtable<Object, Object> getModuleHash() {
		return ModuleHash;
	}
	
	public void setPoolTablename(String poolTablename)
	{
		this.poolTablename = poolTablename;
	}
	
	@SuppressWarnings("unchecked")
	public Hashtable<Object, Object> getPoolHash() 
	{
		try
		{
			Class c = Class.forName("poolint.push.tablename."+CapitalChar.makeFirstCharCapital(poolTablename));
			IntegratePool integrate = (IntegratePool) c.newInstance();
			integrate.setRecentTableObject(recentTableObject);
			integrate.setTableObj(tableObj);
			integrate.setMaster(masterObj);
			poolHash.putAll(integrate.getIntegratePoolHash());
		
			poolVector.addAll(integrate.getPoolVector());
			
			return poolHash;
		} catch (Exception e) {
			System.out.println("Exception::poolint.join::getPoolHash");
			e.printStackTrace();
		}	
		return new Hashtable<Object, Object>();
	}
	
	public Hashtable<Object, Object> getFieldHash() {
		return fieldHash;
	}

	public void setProcess(String process) 
	{
		this.process = process;
	}
}
